package diploma.kisel.modelas.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import diploma.kisel.hibernate.util.HibernateUtil;
import diploma.kisel.modelas.domain.Concept;
import diploma.kisel.modelas.domain.User;
import diploma.kisel.modelas.domain.Workstation;
import diploma.kisel.modelas.repository.api.WorkstationRepository;

public class WorkstationRepositoryImpl implements WorkstationRepository {

	EntityManager em = HibernateUtil.getEm();

	@Override
	public List<Workstation> findAll() {
		List<Workstation> workstations = em.createNamedQuery(
				"Workstation.findAll", Workstation.class).getResultList();
		return workstations;
	}

	@Override
	public Workstation findById(int id) {
		TypedQuery<Workstation> query = em.createNamedQuery(
				"Workstation.findById", Workstation.class);
		query.setParameter("id", id);
		return query.getSingleResult();
	}

	@Override
	public Workstation save(Workstation workstation) {
		em.getTransaction().begin();
		em.persist(workstation);
		em.getTransaction().commit();
		return workstation;
	}

	@Override
	public Workstation update(Workstation workstation) {
		em.getTransaction().begin();
		em.merge(workstation);
		em.getTransaction().commit();
		return workstation;
	}

	@Override
	public void remove(Workstation workstation) {
		em.getTransaction().begin();
		em.remove(workstation);
		em.getTransaction().commit();
	}

	@Override
	public List<Workstation> findByConcept(Concept concept) {
		TypedQuery<Workstation> query = em.createNamedQuery(
				"Workstation.findByConcept", Workstation.class);
		query.setParameter("concept", concept);
		return query.getResultList();
	}
}
